package com.hxd.otc.main;

import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hxd.otc.MainActivity;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.common.SpConfig;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.utils.CommonUtil;
import com.hxd.otc.utils.SpUtil;
import com.hxd.otc.widget.BlocksViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/20 16:37
 * 描  述 :  ${引导页}
 */
public class GuideActivity extends BaseActivity {
    @BindView(R.id.guide_vp)
    BlocksViewPager mViewPager;
    private List<Integer> mImageData;
    private GuideAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_guide;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        mImageData = new ArrayList<>();
//        mImageData.add(R.mipmap.icon_guide_1);
        mImageData.add(R.mipmap.icon_guide_2);
//        mImageData.add(R.mipmap.icon_guide_3);
        if (mAdapter == null) {
            mAdapter = new GuideAdapter();
            mViewPager.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }


    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImageData == null ? 0 : mImageData.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getItemPosition(@NonNull Object object) {
            return POSITION_NONE;
        }

        @NonNull
        @Override
        public View instantiateItem(@NonNull ViewGroup container, int position) {
            View inflate = LayoutInflater.from(GuideActivity.this).inflate(R.layout.item_guide_view, container, false);
            container.addView(inflate);
            ImageView imageView = inflate.findViewById(R.id.item_iv);
            View openView = inflate.findViewById(R.id.open_tv);
            RadioGroup mRadioGroup = inflate.findViewById(R.id.item_rg);
            RadioButton radioButton = (RadioButton) mRadioGroup.getChildAt(position);
            radioButton.setChecked(true);
            imageView.setImageResource(mImageData.get(position));
            if (position == mImageData.size() - 1) {
                openView.setVisibility(View.VISIBLE);
            }
            openView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SpUtil.PERMANENT.putInt(SpConfig.Welcome.GUIDE_VERSION, CommonUtil.getVersionCode());
                    $startActivity(MainActivity.class, true);
                }
            });
            return inflate;
        }
    }

}
